﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using COFE.IO;

namespace COFE.Core.Tasks
{
    public static class IEntryListTaskExtension
    {
        public static Task<IEntryList> GetTask(this IEntryList entryList, Guid parentTaskId, int taskStage, 
            Task<IEntryList> previousTask, object action, string param, CancellationToken token)
        {
            if (!entryList.IsPropertySupported(action, PropertyRetrivalOptions.NotApplied))
                throw new NotSupportedException();

            PropertyPair ppair = entryList.GetProperty(action, PropertyRetrivalOptions.NotApplied);

            var func = (Func<IEntryList, string, IEntryList>)ppair.Value;

            if (previousTask == null)
                return new Task<IEntryList>(
                    () => {
                            //COFETaskShared.ParentTaskId = parentTaskId;
                            //COFETaskShared.TaskStage = taskStage;
                            //COFETaskShared.CancellationToken = token;

                            COFETaskShared.TaskProgressDispatcher.DispatchEvent(entryList, new TaskProgressEventArgs(ProgressType.Start));
                            return func(null, param); 
                    
                    }, token);
            else return previousTask.ContinueWith<IEntryList>(
                (prevTask) =>
                {
                    return func(null, param);
                }

                );
        }

       
    }
}
